package com.safeluck.scannerqcr.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.constraint.solver.SolverVariable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.support.v4.content.ContextCompat;

import com.safeluck.scannerqcr.R;


/**
 * ScannerQCR
 * Created by lzw on 2017/11/28. 11:49:54
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class BottomBarTab extends FrameLayout {

    private ImageView mIcon;//上面的图标
    private TextView mTvTitle;//图标下的文字
    private TextView mTvUnreadCount;//图标下的文字
    private Context mContext;//上下文
    private int mTabPosition = -1;

    public BottomBarTab(@NonNull Context context, @DrawableRes int icon, CharSequence title) {
        this(context,null,icon,title);
    }

    public BottomBarTab(@NonNull Context context, @Nullable AttributeSet attrs,@DrawableRes int icon,CharSequence title) {
        this(context, attrs,0,icon,title);
    }

    public BottomBarTab(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr,@DrawableRes int icon,CharSequence title) {
        super(context, attrs, defStyleAttr);
        init(context,icon,title);
    }

    private void init(Context context,int icon, CharSequence title) {
        this.mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.selectableItemBackgroundBorderless});
        Drawable drawable = typedArray.getDrawable(0);
        setBackgroundDrawable(drawable);
        typedArray.recycle();

        LinearLayout lLcontainer = new LinearLayout(context);
        lLcontainer.setOrientation(LinearLayout.VERTICAL);
        lLcontainer.setGravity(Gravity.CENTER);
        LayoutParams paramsContainer= new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsContainer.gravity= Gravity.CENTER;
        lLcontainer.setLayoutParams(paramsContainer);

        mIcon = new ImageView(context);
        mIcon.setImageResource(icon);
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,27,getResources().getDisplayMetrics());
        LinearLayout.LayoutParams layoutparmas = new LinearLayout.LayoutParams(size,size);
        mIcon.setLayoutParams(layoutparmas);
        mIcon.setColorFilter(ContextCompat.getColor(context,R.color.tab_unselect));
        lLcontainer.addView(mIcon);

        mTvTitle = new TextView(context);
        mTvTitle.setText(title);
        LinearLayout.LayoutParams titleLayoutparmas = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleLayoutparmas.topMargin = dip2px(context,2);
        mTvTitle.setTextSize(10);
        mTvTitle.setTextColor(ContextCompat.getColor(context,R.color.tab_unselect));
        mTvTitle.setLayoutParams(titleLayoutparmas);
        lLcontainer.addView(mTvTitle);

        addView(lLcontainer);

        int min = dip2px(context,20);
        int padding = dip2px(context,5);
        mTvUnreadCount = new TextView(context);
        mTvUnreadCount.setBackgroundResource(R.drawable.bg_msg_bubble);
        mTvUnreadCount.setMinWidth(min);
        mTvUnreadCount.setTextColor(Color.WHITE);
        mTvUnreadCount.setPadding(padding,0,padding,0);
        mTvUnreadCount.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams tvUnreadParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,min);
        tvUnreadParam.gravity = Gravity.CENTER;
        tvUnreadParam.bottomMargin = dip2px(context,17);
        tvUnreadParam.leftMargin = dip2px(context,14);
        mTvUnreadCount.setLayoutParams(tvUnreadParam);
        mTvUnreadCount.setVisibility(View.GONE);

        addView(mTvUnreadCount);


    }

    private int dip2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected){
            mIcon.setColorFilter(ContextCompat.getColor(mContext,R.color.colorPrimary));
            mTvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.colorPrimary));
        }else{
            mIcon.setColorFilter(ContextCompat.getColor(mContext,R.color.tab_unselect));
            mTvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.tab_unselect));
        }
    }

    public void setTabPosition(int position){
        mTabPosition = position;
        if (position == 0){
            setSelected(true);
        }
    }

    public int getTabPosition(){
        return mTabPosition;
    }

    /**
     * s设置未读数量
     * @param unReadCount
     */
    public void setUnreadCount(int unReadCount){
        if (unReadCount <0){
            mTvUnreadCount.setText(String.valueOf(0));
            mTvUnreadCount.setVisibility(View.GONE);
        }else{
            mTvUnreadCount.setVisibility(View.VISIBLE);
            if (unReadCount >99){
                mTvUnreadCount.setText(String.valueOf("99+"));

            }else{
                mTvUnreadCount.setText(String.valueOf(unReadCount));
            }
        }
    }

    /**
     * 获取当前未读消息数量
     * @return
     */
    public int getUnreadCount(){
        int count = 0;
        if (TextUtils.isEmpty(mTvUnreadCount.getText())) {
            return count;
        }
        if (mTvUnreadCount.getText().toString().equals("99+")) {
            return 99;
        }
        try {
            count = Integer.valueOf(mTvUnreadCount.getText().toString());
        } catch (Exception ignored) {
        }
        return count;
    }
}
